//
//  ELStatus.swift
//  SinaWeiBo
//
//  Created by jiwang on 15/12/20.
//  Copyright © 2015年 jiwang. All rights reserved.
//

import UIKit

class ELStatus: NSObject {

    ///cell的缓存行高
    var RowHeight : CGFloat?
    
    ///微博ID
    var id : Int64 = 0
    ///微博来源
    var source : String?
    ///信息内容
    var text : String?
    ///创建时间
    var created_at : String?
    ///转发数
    var reposts_count : Int = 0
    ///评论数
    var comments_count : Int = 0
    ///表态数
    var attitudes_count : Int = 0
    //图片数组
    var pic_urls : [[String : AnyObject]]? {
        didSet{
            
            //判断是否有图片数组
            //没有则返回
            let picCount = pic_urls?.count ?? 0
            
            if picCount == 0 {
                return
            }
            
            //有图片则创建图片链接数组
            storePictureURLs = [NSURL]()
            
            for picture in pic_urls! {
                let image = picture["thumbnail_pic"] as! String
                
                let url = NSURL(string: image)!
                
                storePictureURLs?.append(url)
            }
        }
    }
    
    /// 微博配图的图片连接数组
    var storePictureURLs: [NSURL]?
    
    //用户模型
    var user : ELUserModel?
    
    init(dict : [String : AnyObject]) {
        super.init()
        
        self.setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override func setValue(value: AnyObject?, forKey key: String) {
        //将user字典提取出来重新赋值
        if key == "user" {
            
            if let dict = value as? [String : AnyObject] {
                
                self.user = ELUserModel(dict: dict)
            }
            //赋值完返回
            return
        }
        
        super.setValue(value, forKey: key)
    }
    
    /// 对象打印方法
    override var description: String {
        let properties = ["id", "created_at", "text", "source", "pic_urls", "reposts_count", "comments_count", "attitudes_count","user"]
        
        // 根据数组中的元素,去模型中获取对应的值,并生成字典
        return "\n\t:微博模型: \(dictionaryWithValuesForKeys(properties))"
    }

    class func loadUserStatuses (finished : (list : [ELStatus]?,error : NSError?) -> ()) {
        
        ELNetWorkTool.shareNetWork.loadStatuses { (result, error) -> () in
            if error != nil {
                print("读取失败")
                return
            }
            
            //没有错则表示有数据
            //返回的是 一个字典 ,拿出里面的statuses数组，里面保存的是一个字典
            if let statusArray = result?["statuses"] as? [[String : AnyObject]] {
                
                var status = [ELStatus]()
                
                for dict in statusArray {
                    
                    let object = ELStatus(dict: dict)
                    status.append(object)
                }
                
                finished(list: status, error: nil)
            }else {
                finished(list: nil, error: nil)
            }
        }
    }
    
}
